package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.Setmeal;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Guoyh
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;




    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);

        return R.success("用户下单成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> pageDto = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo, pageDto, "records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list1 = new ArrayList<>();
        records.forEach(
                new Consumer<Orders>() {
                    @Override
                    public void accept(Orders order) {
                        OrdersDto ordersDto = new OrdersDto();
                        Long numberId = Long.valueOf(order.getNumber());

                        LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
                        queryWrapper1.eq(OrderDetail::getOrderId, numberId);
                        List<OrderDetail> orderDetails = orderDetailService.list(queryWrapper1);

                        Integer count = 0;
                        for (OrderDetail orderDetail : orderDetails) {
                            Integer number = orderDetail.getNumber();
                            count += number;
                        }
                        ordersDto.setSumNum(count);
                        ordersDto.setOrderDetails(orderDetails);
                        BeanUtils.copyProperties(order, ordersDto);
                        list1.add(ordersDto);
                    }
                }
        );
        pageDto.setRecords(list1);
        return R.success(pageDto);
    }
}

